/*
 * Copyright 2010 Donny A. Wijaya
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.uberium.lazy.generate;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * The GenerateMojo class. The basic class for all MOJO available in this package.
 * 
 * @author Donny A. Wijaya
 * @version 0.0.9
 * @since April 2010
 */
public abstract class GenerateMojo extends AbstractMojo {
	// The context
	private final ApplicationContext context;

	// The key or the generation type
	private String key;

	/**
     * Name of the generated item
     * @parameter expression="${name}"
     * @required
     */
	private String name = "";

	/**
     * Package of the generated item
     * @parameter expression="${package}"
     * @required
     */
	private String _package = "";
	
	/**
     * Type of the generated item.
     * @parameter expression="${type}"
     */
	private String type = "";

	/**
	 * Instantiate a GenerateMojo object.
	 *
	 * @param key
	 * @param contextFile
	 */
	public GenerateMojo(String key, String contextFile) {
		this(key, new String[] {contextFile});
	}
	
	/**
	 * Instantiate a GenerateMojo object.
	 *
	 * @param key
	 * @param contextFiles
	 */
	public GenerateMojo(String key, String[] contextFiles) {
		Thread.currentThread().setContextClassLoader(GenerateMojo.class.getClassLoader());
		this.key = key;
		context = new ClassPathXmlApplicationContext(contextFiles);
		((ConfigurableApplicationContext) context).registerShutdownHook();
	}

	/**
	 * Get the generator.
	 * 
	 * @param name
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected final Generator<GenerateMojo> getGenerator(String name) {		
		return (Generator<GenerateMojo>) this.context.getBean(name, Generator.class);
	}
	
	/**
	 * Get the key of the generator
	 * @return
	 */
	public String getKey() {
		return key;
	}
	
	/**
	 * Get the name of the entity.
	 * 
	 * @return entity
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Get the package of the entity.
	 * 
	 * @return entity
	 */
	public String getPackage() {
		return _package;
	}
	
	/**
	 * Get the type of the generation
	 * @return
	 */
	public String getType() {
		return type;
	}
	
	/**
	 * Set the type of the generation
	 * @param type
	 */
	public void setType(String type) {
		this.type = type;
	}
	
	/**
	 * Execute the generator.
	 */
	public void execute() throws MojoExecutionException, MojoFailureException {		
		try {
			getGenerator(getKey()).execute(this);
		} catch (Exception e) {
			getLog().error(e);
		}
	}
}